<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\UCenterController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AIndexController extends Controller{
    
    //头部
//    public function header(){
//        $num = DB::table('waihui_member')->count();
//        $allProfit = DB::table('waihui_order')
//                    ->where([['isstop','=',0],['ispersonal','=',0]])
//                    ->sum('final_point');
//        $allProfit = round($allProfit,2);
//        return view('admin.adminheader',compact('num','allProfit'));
//    }
//    
    
    //后台首页
    public function index(Request $request){
        $type              = trim($request->type); // 类型
        $timestart         = strtotime(trim($request->timestart)); //开始时间
        $timeend           = strtotime(trim($request->timeend))+3600;//结束时间
        $finalpointstart   = $request->finalpointstart; //总赢开始
        $finalpointend     = $request->finalpointend;//总赢结束
        $orderstart        = intval(trim($request->orderstart));//单量开始
        $orderend          = intval(trim($request->orderend));//单量开始
        $ordersort         = $request->ordersort;//按单量排序
        $finalpointsort    = $request->finalpointsort;//按总赢排序
        $ordersortstr      = $ordersort==2?'asc':'desc';
        $finalpointsortstr = $finalpointsort==2?'asc':'desc';
        if($ordersort){
            $strname = "ordernum";
            $strsort = "$ordersortstr";
        }else{
            $strname = "final_point";
            $strsort = "$finalpointsortstr";
        }
        $where[0]   = ['o.isstop','=',0];
        $where[1]   = ['o.ispersonal','=',0];
        if($type == 1){
            $where[2] = ['m.isanalyst','=',1];
        }elseif($type == 2){
            $where[2] = ['m.isanalyst','!=',1];
        }
        $timestart = $timestart == false?0:$timestart;
        $timeend   = $timeend == 3600?time():$timeend;
        if($finalpointstart != ''){
            $finalstartstr = "SUM(o.final_point)>=$finalpointstart";
        }else{
            $finalstartstr = "1=1";
        }
        if($finalpointend != ''){
            $finalstr = "SUM(o.final_point)<=$finalpointend";
        }else{
            $finalstr = "1=1";
        }
        if($orderend){
            $orderstr = "COUNT(o.id)<=$orderend";
        }else{
            $orderstr = "1=1";
        }
        $result = DB::table('waihui_member as m')
                    ->select('m.username','m.tel', DB::raw('ROUND(SUM(o.final_point),2) as final_point'), DB::raw('COUNT(o.id) as ordernum'), DB::raw("(ROUND(count(case when o.trade_status='1' then 1 end) / count(o.tel),2))*100 AS winrate"), DB::raw("ROUND((SUM(final_point)/count(o.tel)),2) AS single_final_point"))
                    ->join('waihui_order as o', 'm.tel', 'o.tel')
                    ->where($where)
                    ->whereBetween('o.end_time', [$timestart, $timeend])
                    ->havingRaw($finalstartstr)
                    ->havingRaw($finalstr)
                    ->havingRaw("COUNT(o.id)>=$orderstart")
                    ->havingRaw($orderstr)
                    ->orderBy("$strname","$strsort")
                    ->groupBy('m.tel')
                    ->paginate(10);
        foreach ($result as $k=>$v){
            $result[$k]->chicang      = DB::table('waihui_order')->where([['tel','=',$v->tel],['isstop','=',1]])->count();
            $result[$k]->weekordernum = DB::table('waihui_order')->where([['tel','=',$v->tel],['isstop','=',0],['begin_time','>',strtotime("-1 week")]])->count();//近一周的策略总数
            $wenDindZhi     = UCenterController::wenDindZhi($v->tel);
            $result[$k]->wdzs           = $wenDindZhi['wenDing']; //稳定指数
            $result[$k]->fengXianBi    = $wenDindZhi['fengXianBi']; //风险比
            $huiche         = UCenterController::maxDada($v->tel);
            $result[$k]->huiche         = $huiche['huiche']; //最大回撤
            $result[$k]->zhangfu        = $huiche['zhangfu']; //最大涨幅
        }
        return view('admin.index',compact('result','type','timestart','timeend','finalpointstart','finalpointend','orderstart','orderend','finalpointsort','ordersort'));
    }
    // 后台条件查询中的---详情页
    public function indexDetail(){
        return view('admin.detail');
    }
    // 后台客户查询
    public function customerCX(Request $request){
        $username = $request->username;
        $userinfo = DB::table('waihui_member as m')
                        ->join('waihui_member_info as mi','m.id','mi.userid')
                        ->where('m.username','=',"$username")
                        ->select('m.username','m.grade','m.photo','m.isanalyst','m.reg_time','m.tel','mi.username as realname','mi.idcard','mi.idcardimg','mi.idcardimgf','mi.card_auth_time','mi.diplomas','mi.school','mi.province','mi.city','mi.country')
                        ->first();
        $bankarr = DB::table('waihui_member as m')
                        ->join('waihui_bank as b','m.id','b.userid')
                        ->select('b.bank_number','b.bank','b.bank_branch','b.bank_auth_time')
                        ->where('m.username','=',"$username")
                        ->get()->toArray();
        return view('admin.customercx',compact('userinfo','bankarr','username'));
    }
    
    // 后台持仓查询
     public function chicangCX(Request $request){
        $searchname = $username = htmlspecialchars(trim($request->username));
        if($username != ''){
            $names = $userData = array_filter(explode(',', $username)); //过滤数组中值为空的
            if(!$userData){
                $str = 'sex';
                $userData = array(1,2);
            }else{
                $str = 'username';
            }
        }else{
            $str = "sex";
            $userData = array(1,2);
        }
        //DB::connection()->enableQueryLog();
        
        $data = DB::table('waihui_order as o')
                ->leftJoin('waihui_member as m','o.tel','=','m.tel')
                ->select('username','tradeid','typename','begin_point',DB::raw('FROM_UNIXTIME(begin_time,"%Y-%m-%d %H:%i:%s") as begin_time'),'profit_point','loss_point','target_profit_point','target_loss_point','buy_status')
                ->where([['isstop','=',1],['ispersonal','=',0]])
                ->whereIn($str,$userData)
                ->groupBy('o.tradeid')
                ->paginate(10);
        //$queries = DB::getQueryLog(); 
        //dd($data);
        return view('admin.chicangcx',compact('data','names','searchname'));
    }
    
    // 后台订单查询
    public function dingdanCX(Request $request){
        $username = htmlspecialchars(trim($request->username)) ? htmlspecialchars(trim($request->username)) : '';
        $tradeid = htmlspecialchars(trim($request->tradeid)) ? htmlspecialchars(trim($request->tradeid)) : '';
        $data = '';
        $msg = '';
        if($username != '' || $tradeid != ''){
            if($username != ''){ 
                $tel = DB::table('waihui_member')->where('username',$username)->value('tel'); //判断该用户名是否存在
                if($tel){
                    $data = DB::table('waihui_order as o')
                            ->join('waihui_member as m','o.tel','=','m.tel')
                            ->select('m.username','o.tradeid','typename','begin_point','end_point',
                                    DB::raw('FROM_UNIXTIME(begin_time,"%Y-%m-%d %H:%i:%s") as begin_time'),
                                    DB::raw('FROM_UNIXTIME(end_time,"%Y-%m-%d %H:%i:%s") as end_time'),
                                    'final_point','buy_status','profit_point','loss_point','stop_status','isstop')
                            ->where([['o.tel','=',$tel],['ispersonal','=',0]])
                            ->orderBy('isstop','desc','begin_time','desc')
                            ->paginate(10);
                }else{
                    $msg = '该用户名不存在';
                }
                
            }else{ //按订单号查询
                $data = DB::table('waihui_order as o')
                        ->join('waihui_member as m','o.tel','=','m.tel')
                        ->select('m.username','o.tradeid','typename','begin_point','end_point',
                                DB::raw('FROM_UNIXTIME(begin_time,"%Y-%m-%d %H:%i:%s") as begin_time'),
                                DB::raw('FROM_UNIXTIME(end_time,"%Y-%m-%d %H:%i:%s") as end_time'),
                                'final_point','buy_status','profit_point','loss_point','stop_status','isstop')
                        ->where([['tradeid','=',$tradeid],['ispersonal','=',0]])
                        ->get();
                if(count($data) == 0){
                    $msg = '该订单号不存在';
                }
            }
        }
        
        return view('admin.dingdancx',compact('data','msg','username','tradeid'));
    }
}
